package com.gexin.model.user;

import com.gexin.model.user.base.BaseUserCoupon;

import java.util.List;

/**
 * 业务名称:  t_user_coupon(用户优惠劵表) 
 */
@SuppressWarnings("serial")
public class UserCoupon extends BaseUserCoupon<UserCoupon> {
	public static final UserCoupon dao = new UserCoupon();

	public static final Integer STATE_WEISHIYONG = 0;
	public static final Integer STATE_YISHIYONG = 1;
	public static final Integer STATE_YIGUOQI = 2;
	public static final Integer STATE_SHIYONGZHONG = 2;

	/**
	 * 根据用户id和优惠券id查询用户领取的优惠券
	 * @param userId 用户id
	 * @param couponId 优惠券id
	 */
	public UserCoupon findByUserIdAndCoupon(Integer userId , String couponId) {
		return findFirst("select * from t_user_coupon where user_id=? and coupon_no=?",userId,couponId);
	}

	public List<UserCoupon> findListByCoupon(String couponId) {
		return find("select * from t_user_coupon where coupon_no=?",couponId);
	}

	public List<UserCoupon> findAllStatusAndUserId(int status , Integer userId) {
		StringBuffer sb = new StringBuffer();

		sb.append(" select userCoupon.*,coupon.title,coupon.used,coupon.product_id,coupon.product_type_id  from t_user_coupon userCoupon ");
		sb.append(" LEFT JOIN t_coupon coupon ON coupon.id=userCoupon.coupon_no ");
		sb.append(" WHERE userCoupon.use_state=? AND user_id=? and now() BETWEEN userCoupon.use_date AND ending_date");
		return find(sb.toString(),status,userId);
	}
}
